package cn.wildfire.chat.app.login;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;

import butterknife.BindView;
import butterknife.OnClick;
import cn.wildfire.chat.kit.AppService;
import cn.wildfire.chat.kit.WfcBaseActivity;

import com.wildfire.chat.R;
import cn.wildfirechat.model.UserInfo;

public class UpdatePasswordActivity extends WfcBaseActivity {
    @BindView(R.id.etPasswordOld)
    EditText etPasswordOld;
    @BindView(R.id.etPassword)
    EditText etPassword;
    @BindView(R.id.etPasswordAgain)
    EditText etPasswordAgain;
    private UserInfo userInfo;

    @Override
    protected int contentLayout() {
        return R.layout.activity_update_password;
    }

    @Override
    protected void afterViews() {
        super.afterViews();
        userInfo = getIntent().getParcelableExtra("userInfo");
        if (!isDarkTheme()) {
            setTitleBackgroundResource(cn.wildfire.chat.kit.R.color.white, false);
        }
    }

    private void updatePassword() {
        String passwordOld = etPasswordOld.getText().toString();
        String password = etPassword.getText().toString();
        String passwordAgain = etPasswordAgain.getText().toString();
        if (TextUtils.isEmpty(passwordOld)){
            return;
        }
        if (TextUtils.isEmpty(password)){
            return;
        }
        if (TextUtils.isEmpty(passwordAgain)){
            return;
        }
        if (!password.equals(passwordAgain)){
            Toast.makeText(UpdatePasswordActivity.this, "两次输入密码不一致", Toast.LENGTH_SHORT).show();
            return;
        }

        MaterialDialog dialog = new MaterialDialog.Builder(this)
                .content("修改中...")
                .progress(true, 100)
                .cancelable(false)
                .build();
        dialog.show();

        AppService.Instance().updatePassword(userInfo.uid,passwordOld, password, passwordAgain,new AppService.CustomSimpleCallback() {
            @Override
            public void onUiSuccess(String result) {
                if (isFinishing()) {
                    return;
                }
                dialog.dismiss();
                Toast.makeText(UpdatePasswordActivity.this, "修改成功" , Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(UpdatePasswordActivity.this, SMSLoginActivity.class);
                startActivity(intent);
                finish();
            }

            @Override
            public void onUiFailure(int code, String msg) {
                if (isFinishing()) {
                    return;
                }
                Toast.makeText(UpdatePasswordActivity.this, "修改失败：" + code + " " + msg, Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });
    }
    @OnClick({R.id.updateButton})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.updateButton:
                updatePassword();
                break;
        }
    }
}